<?php
$loader->requireOnce('includes/ManagerDashboard/Portlet.class.php');
class PortletPatientAge extends Portlet {
	protected $minAge;
	protected $maxAge;
	protected $step;
	
	function PortletPatientAge($name, $dateRange, $minAge, $maxAge, $step) {
		$this->name = $name;
		$this->dateRange = $dateRange;
		$this->minAge = $minAge;
		$this->maxAge = $maxAge;
		$this->step = $step;
		$this->_db = $GLOBALS['frame']['adodb']['db'];
	}
	
	protected function collectData() {
		$curYear = date('Y');
		$step = $this->step;
		$sql = "SELECT count(DISTINCT(patient_id)) total, ((ceil((($curYear - year(person.date_of_birth)) + 1) / $step)) * $step) range
				FROM ticket INNER JOIN person ON (person.person_id = ticket.patient_id)
				WHERE date(closed) <= '$this->endDate'
				AND date(closed) >= '$this->startDate'
				GROUP BY range";
		$result = $this->_db->GetAll($sql);
		$i = 0;
		$upperLimitAge = 0;
		foreach ($result as $row){
			// Remember to use INTEGER data for the chart value (string will not be rendered)
			if ($row['range'] <= $this->maxAge) {
				$this->values[$i] = new OFC_Charts_Bar_Glass_Value((int)($row['total']), '#3030D0');
				$label = ($row['range'] - $step + 1) . " - " . $row['range'];
				$this->values[$i]->set_tooltip(L10nHelper::_("%s Patient%sAge in range %s",L10nHelper::renderNumber($row['total']),"<br>",$label));
				$this->xLabels[$i] = $label;
				$i++;
			} else {
				$upperLimitAge += $row['total'];
				$label = '> ' . $this->maxAge;
				$this->values[$i] = new OFC_Charts_Bar_Glass_Value($upperLimitAge, '#3030D0');
				$this->values[$i]->set_tooltip(L10nHelper::_("%s Patient%sAge in range %s","#val#","<br>",$label));
				$this->xLabels[$i] = $label;		
			}
		}		
	}
	
	protected function buildChart() {
		// Create chart object and set backgound color
		$this->chart = new OFC_Chart();
		$this->chart->set_bg_colour("#FEFEFE");
		// Create chart title object and pass css param
		$title = new OFC_Elements_Title($this->name);
		$title->set_style("font-size: 13px; font-weight: bold; padding-bottom: 10px; color: #00248E");
		// Create chart object (each type is an object)
		$glass_bar = new OFC_Charts_Bar_Glass();
		// Set chart value
		$glass_bar->set_values($this->values);
		// Set lable, style for X axis
		$x_axis = new OFC_Elements_Axis_X();
		$x_axis->set_labels_from_array($this->xLabels);
		$x_axis->labels->set_rotate('diagonal');
		$x_axis->labels->set_size(9);
		$x_axis->set_colours("#555555", "#FEFEFE");
		// Set value range, step and style for Y axis
		$y_axis = new OFC_Elements_Axis_Y();
		$y_axis->set_range(0, 700, 50);
		$y_axis->set_colours("#555555", "#DDDDDD");
		// Add all of them to chart object
		$this->chart->set_title($title);
		$this->chart->add_element($glass_bar);
		$this->chart->set_x_axis($x_axis);
		$this->chart->set_y_axis($y_axis);
	}
	
}
?>